﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Job.Core
{
    public class PagingHelper
    {
        /// <summary>
        /// 分页工具栏
        /// href='{0}{2}page={1}'
        /// </summary>
        /// </summary>
        /// <returns></returns>
        /// <param name="PageCount">查询数据的总记录数</param>
        /// <param name="PageSize">每页显示的记录条数</param>
        /// <param name="CurrentPage">当前页码</param>
        /// <param name="strUrl">URL地址</param>
        /// <param name="strPageNum">分页条一次性显示的页数</param>
        public static string Pagination(int PageCount, int PageSize, int CurrentPage, string strUrl, int intDisplayNum)
        {
            bool isBln = false;

            if (strUrl.Contains("?"))
                isBln = true;
            if (strUrl.Contains("?page="))
            {
                isBln = false;
                strUrl = strUrl.Substring(0, strUrl.IndexOf("?page="));
            }
            if (strUrl.Contains("&page="))
            {
                isBln = true;
                strUrl = strUrl.Substring(0, strUrl.IndexOf("&page="));
            }

            StringBuilder strHtml = new StringBuilder();

            //获取总页数
            int intTemp = PageCount % PageSize;
            int PageNum = (intTemp == 0 ? PageCount / PageSize : PageCount / PageSize + 1);

            //当总页数大于1页时才生成pageBar
            if (PageNum > 1)
            {
                //判断总页数是否大于pageBar单次显示的页码总数
                if (PageNum > intDisplayNum)
                {
                    int intCount = 0;
                    int intTempNew = CurrentPage % intDisplayNum;
                    int intNum = (intTempNew == 0 ? CurrentPage / intDisplayNum : CurrentPage / intDisplayNum + 1);
                    int intNumNew = (intNum * intDisplayNum <= PageNum ? intNum * intDisplayNum : PageNum);

                    if (CurrentPage > intDisplayNum)
                        strHtml.AppendFormat("<a href='{0}{2}page={1}'><<</a>", strUrl, (intNum * intDisplayNum - intDisplayNum), isBln ? "&" : "?");

                    for (int j = (intNum - 1) * intDisplayNum + 1; j <= intNumNew; j++)
                    {
                        if (CurrentPage != j)
                            strHtml.AppendFormat("<a href='{0}{2}page={1}'>{1}</a>", strUrl, j, isBln ? "&" : "?");
                        else
                            strHtml.AppendFormat("<a href='{0}{2}page={1}' class=\"active\">{1}</a>", strUrl, j, isBln ? "&" : "?");
                        intCount = j;
                    }

                    if (intCount < PageNum)
                        strHtml.AppendFormat("<a href='{0}{2}page={1}'>>></a>", strUrl, intCount + 1, isBln ? "&" : "?");
                }
                else
                {
                    for (int i = 1; i <= PageNum; i++)
                    {
                        if (CurrentPage != i)
                            strHtml.AppendFormat("<a href='{0}{2}page={1}'>{1}</a>", strUrl, i, isBln ? "&" : "?");
                        else
                            strHtml.AppendFormat("<a href='{0}{2}page={1}' class=\"active\">{1}</a>", strUrl, i, isBln ? "&" : "?");
                    }
                }
            }

            return strHtml.ToString();
        }

        public static string JsPagination(int PageCount, int PageSize, int CurrentPage, int intDisplayNum)
        {
            StringBuilder strHtml = new StringBuilder();

            //获取总页数
            int PageNum = PageCount % PageSize == 0 ? PageCount / PageSize : (PageCount / PageSize + 1);

            //当总页数大于1页时才生成pageBar
            if (PageNum > 1)
            {
                //判断总页数是否大于pageBar单次显示的页码总数
                if (PageNum > intDisplayNum)
                {
                    int intCount = 0;
                    int intNum = CurrentPage % intDisplayNum == 0 ? CurrentPage / intDisplayNum : (CurrentPage / intDisplayNum + 1);
                    int intNumNew = (intNum * intDisplayNum <= PageNum ? intNum * intDisplayNum : PageNum);

                    if (CurrentPage > intDisplayNum)
                        strHtml.AppendFormat(@"<a href=""#""><<</a>", intNum * intDisplayNum - intDisplayNum);

                    for (int j = (intNum - 1) * intDisplayNum + 1; j <= intNumNew; j++)
                    {
                        if (CurrentPage != j)
                            strHtml.AppendFormat(@"<a href=""#"">{0}</a>", j);
                        else
                            strHtml.AppendFormat(@"<a href=""#"" class=""active"">{0}</a>", j);
                        intCount = j;
                    }

                    if (intCount < PageNum)
                        strHtml.AppendFormat(@"<a href=""#"">>></a>", intCount + 1);
                }
                else
                {
                    for (int i = 1; i <= PageNum; i++)
                    {
                        if (CurrentPage != i)
                            strHtml.AppendFormat(@"<a href=""#"">{0}</a>", i);
                        else
                            strHtml.AppendFormat(@"<a href=""#"" class=""active"">{0}</a>", i);
                    }
                }
            }

            return strHtml.ToString();
        }

        /// <summary>
        /// href="{0}/{1}{2}.htm"
        /// </summary>
        public static string HtmlPagination(int Count, int PageSize, int PageIndex, string strUrl, string strPageKey, int intDisplayNum)
        {
            StringBuilder sbPageBar = new StringBuilder();

            //获取总页数
            int intTemp = Count % PageSize;
            int PageNum = (intTemp == 0 ? Count / PageSize : Count / PageSize + 1);

            //当总页数大于1页时才生成pageBar
            if (PageNum > 1)
            {
                //判断总页数是否大于pageBar单次显示的页码总数
                if (PageNum > intDisplayNum)
                {
                    int intCount = 0;
                    int intTempNew = PageIndex % intDisplayNum;
                    int intNum = (intTempNew == 0 ? PageIndex / intDisplayNum : PageIndex / intDisplayNum + 1);
                    int intNumNew = (intNum * intDisplayNum <= PageNum ? intNum * intDisplayNum : PageNum);

                    if (PageIndex > intDisplayNum)
                        sbPageBar.Append(string.Format("<a href=\"{0}/{1}{2}.htm\"><<</a>", strUrl, strPageKey, (intNum * intDisplayNum - intDisplayNum)));

                    for (int j = (intNum - 1) * intDisplayNum + 1; j <= intNumNew; j++)
                    {
                        if (PageIndex != j)
                            sbPageBar.Append(string.Format("<a href=\"{0}/{1}{2}.htm\">{2}</a>", strUrl, strPageKey, j));
                        else
                            sbPageBar.Append(string.Format("<a href=\"{0}/{1}{2}.htm\" class=\"active\">{2}</a>", strUrl, strPageKey, j));
                        intCount = j;
                    }

                    if (intCount < PageNum)
                        sbPageBar.Append(string.Format("<a href=\"{0}/{1}{2}.htm\">>></a>", strUrl, strPageKey, intCount + 1));
                }
                else
                {
                    for (int i = 1; i <= PageNum; i++)
                    {
                        if (PageIndex != i)
                            sbPageBar.Append(string.Format("<a href=\"{0}/{1}{2}.htm\">{2}</a>", strUrl, strPageKey, i));
                        else
                            sbPageBar.Append(string.Format("<a href=\"{0}/{1}{2}.htm\" class=\"active\">{2}</a>", strUrl, strPageKey, i));
                    }
                }
            }

            return sbPageBar.ToString();
        }

        /// <summary>
        /// href="string.Format(strUrl, j)"
        /// </summary>
        public static string HtmlPaginationBySearch(int PageCount, int PageSize, int CurrentPage, string strUrl, int intDisplayNum)
        {
            StringBuilder strHtml = new StringBuilder();

            //获取总页数
            int PageNum = PageCount % PageSize == 0 ? PageCount / PageSize : (PageCount / PageSize + 1);

            //当总页数大于1页时才生成pageBar
            if (PageNum > 1)
            {
                //判断总页数是否大于pageBar单次显示的页码总数
                if (PageNum > intDisplayNum)
                {
                    int intCount = 0;
                    int intNum = CurrentPage % intDisplayNum == 0 ? CurrentPage / intDisplayNum : (CurrentPage / intDisplayNum + 1);
                    int intNumNew = (intNum * intDisplayNum <= PageNum ? intNum * intDisplayNum : PageNum);

                    if (CurrentPage > intDisplayNum)
                        strHtml.AppendFormat(@"<a href=""{0}""><<</a>", string.Format(strUrl, intNum * intDisplayNum - intDisplayNum));

                    for (int j = (intNum - 1) * intDisplayNum + 1; j <= intNumNew; j++)
                    {
                        if (CurrentPage != j)
                            strHtml.AppendFormat(@"<a href=""{0}"">{1}</a>", string.Format(strUrl, j), j);
                        else
                            strHtml.AppendFormat(@"<a href=""{0}"" class=""active"">{1}</a>", string.Format(strUrl, j), j);
                        intCount = j;
                    }

                    if (intCount < PageNum)
                        strHtml.AppendFormat(@"<a href=""{0}"">>></a>", string.Format(strUrl, intCount + 1));
                }
                else
                {
                    for (int i = 1; i <= PageNum; i++)
                    {
                        if (CurrentPage != i)
                            strHtml.AppendFormat(@"<a href=""{0}"">{1}</a>", string.Format(strUrl, i), i);
                        else
                            strHtml.AppendFormat(@"<a href=""{0}"" class=""active"">{1}</a>", string.Format(strUrl, i), i);
                    }
                }
            }

            return strHtml.ToString();
        }

        public static string HtmlPaginationForUserList(int Count, int PageSize, int PageIndex, string strTypeName, string strFolderName, int intFolderID, int intDisplayNum, string strSimpleName, bool blList)
        {
            string strViewState = "";
            if (!blList)
                strViewState = "v";

            StringBuilder sbPageBar = new StringBuilder();

            //获取总页数
            int intTemp = Count % PageSize;
            int PageNum = (intTemp == 0 ? Count / PageSize : Count / PageSize + 1);

            //当总页数大于1页时才生成pageBar
            if (PageNum > 1)
            {
                //判断总页数是否大于pageBar单次显示的页码总数
                if (PageNum > intDisplayNum)
                {
                    int intCount = 0;
                    int intTempNew = PageIndex % intDisplayNum;
                    int intNum = (intTempNew == 0 ? PageIndex / intDisplayNum : PageIndex / intDisplayNum + 1);
                    int intNumNew = (intNum * intDisplayNum <= PageNum ? intNum * intDisplayNum : PageNum);

                    if (PageIndex > intDisplayNum)
                        sbPageBar.Append(string.Format("<a href=\"/{4}/{0}-{5}list-p{1}-{2}/{3}.htm\"><<</a>", strTypeName, (intNum * intDisplayNum - intDisplayNum), intFolderID, strFolderName, strSimpleName, strViewState));

                    for (int j = (intNum - 1) * intDisplayNum + 1; j <= intNumNew; j++)
                    {
                        if (PageIndex != j)
                            sbPageBar.Append(string.Format("<a href=\"/{4}/{0}-{5}list-p{1}-{2}/{3}.htm\">{1}</a>", strTypeName, j, intFolderID, strFolderName, strSimpleName, strViewState));
                        else
                            sbPageBar.Append(string.Format("<a href=\"/{4}/{0}-{5}list-p{1}-{2}/{3}.htm\" class=\"active\">{1}</a>", strTypeName, j, intFolderID, strFolderName, strSimpleName, strViewState));
                        intCount = j;
                    }

                    if (intCount < PageNum)
                        sbPageBar.Append(string.Format("<a href=\"/{4}/{0}-{5}list-p{1}-{2}/{3}.htm\">>></a>", strTypeName, intCount + 1, intFolderID, strFolderName, strSimpleName, strViewState));
                }
                else
                {
                    for (int i = 1; i <= PageNum; i++)
                    {
                        if (PageIndex != i)
                            sbPageBar.Append(string.Format("<a href=\"/{4}/{0}-{5}list-p{1}-{2}/{3}.htm\">{1}</a>", strTypeName, i, intFolderID, strFolderName, strSimpleName, strViewState));
                        else
                            sbPageBar.Append(string.Format("<a href=\"/{4}/{0}-{5}list-p{1}-{2}/{3}.htm\" class=\"active\">{1}</a>", strTypeName, i, intFolderID, strFolderName, strSimpleName, strViewState));
                    }
                }
            }

            return sbPageBar.ToString();
        }
    }
}
